<!-- 楼栋管理             -->
<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <!-- <div>小区： {{ neighborId }}</div> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['business:bldg:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['business:bldg:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['business:bldg:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['business:bldg:export']">导出</el-button>
      </el-col>
      <right-toolbar :search="false" :showSearch.sync="showSearch" @queryTable="getList"
        :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bldgList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="100" align="center" v-if="getColumn('index')">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="主键" align="center" prop="id" v-if="getColumn('id')" /> -->
      <el-table-column label="楼栋号" align="center" prop="buildingName" v-if="getColumn('buildingName')" />
      <el-table-column label="引入管数量" align="center" prop="pipeNum" v-if="getColumn('pipeNum')" />
      <el-table-column label="架空管长度(m)" align="center" prop="overheadPipeLength"
        v-if="getColumn('overheadPipeLength')" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="getColumn('operate')">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:bldg:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['business:bldg:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :pageSizes="pageSizes" :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改楼栋管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="楼栋号" prop="buildingName">
          <el-input v-model="form.buildingName" placeholder="请输入楼栋号" />
        </el-form-item>
        <el-form-item label="小区id" prop="neighborId">
          <el-input v-model="form.neighborId" placeholder="请输入小区id" />
        </el-form-item>
        <el-form-item label="引入管数量" prop="pipeNum">
          <el-input v-model="form.pipeNum" placeholder="请输入引入管数量" />
        </el-form-item>
        <el-form-item label="架空管长度(m)" prop="overheadPipeLength">
          <el-input v-model="form.overheadPipeLength" placeholder="请输入架空管长度(m)" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBldg, getBldg, delBldg, addBldg, updateBldg } from "@/api/business/bldg";
// import { neighborId } from "@/api/business/ledger";
export default {
  //获取参数 (注意放置位置)  父组件传过来的值可以直接引用
  props: ['neighborId'],
  name: "Bldg",
  //  子组件传值给父组件   可以定义一个函数，点击触发事件emit上传函数， 父组件接到函数
  //  组件用@绑定函数名，在编写一个方法实现属性的change
  // emits:['neighborId'],
  // props: {
  //   neighborId: {                             可以定义多个属性
  //     type: Number,
  //     default: null
  //   }
      //  book:{
      //   XXXX
      //  } 
  // },

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 楼栋管理表格数据
      bldgList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        neighborId: null,
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        buildingName: [
          { required: true, message: "楼栋号不能为空", trigger: "blur" }
        ],
        neighborId: [
          { required: true, message: "小区id不能为空", trigger: "blur" }
        ],
      },
      // 列信息
      columns: [
        { key: 'index', label: `序号`, visible: true },
        { key: 'id', label: '主键', visible: true },
        { key: 'buildingName', label: '楼栋号', visible: true },
        { key: 'pipeNum', label: '引入管数量', visible: true },
        { key: 'overheadPipeLength', label: '架空管长度(m)', visible: true },
        { key: 'operate', label: `操作`, visible: true }
      ],
      // 分页参数
      pageSizes: [10, 50, 100, 500, 1000]
    };
  },
  //   监听父组件传过来的值
  watch: {
    // 当 neighborId 发生变化时执行此函数
    neighborId(newVal, oldVal) {
      console.log('监听到了吗;');
      if (newVal != oldVal) {
        this.getList();
        console.log('我听到了哦!*__*', newVal);
      }
    },
  },

  created() {
    this.getList();
  },

  beforeUpdate() {
    // 在组件更新之前执行
    // this.getList();
  },
  updated() {
    // 在组件更新之后执行


  },

  methods: {

    /** 查询楼栋管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.neighborId = this.neighborId;
      console.log('查询参数:', this.queryParams);
      listBldg(this.queryParams).then(response => {
        this.bldgList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        buildingName: null,
        neighborId: null,
        pipeNum: null,
        overheadPipeLength: null,
        delFlag: null,
        createUser: null,
        createBy: null,
        createTime: null,
        updateUser: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.neighborId = this.neighborId;
      this.title = "添加楼栋管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
     
      getBldg(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改楼栋管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBldg(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBldg(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      console.log('删除参数1:', ids);
      this.$modal.confirm('是否确认删除选中的数据项？').then(function () {
        return delBldg(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/bldg/export', {
        ...this.queryParams
      }, `bldg_${new Date().getTime()}.xlsx`)
    },
    /** 列是否显示 */
    getColumn(key) {
      const res = this.columns.filter(item => item.key === key);
      if (res.length >= 1) {
        return res[0].visible
      } else {
        return false
      }
    }
  }
};
</script>
